import java.util.*;

public class 二叉树的后序遍历 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        dfs(root,ans);
        return ans;
    }
    public void dfs (TreeNode root,List<Integer> ans) {
        if (root == null) {
            return;
        }
        dfs(root.left,ans);
        dfs(root.right,ans);
        ans.add(root.val);
    }
}
